{% extends "layout.html" %}
{% block title %}Pool{% endblock %}
{% block content %}

<h1>{{ pool.name }}</h1>

<h2>Configuration</h2>
<div class="attributes">
  <form action="{{ base_path }}/config-update/pool/{{ pool.name }}" method="POST">
    <div>Worker type: {{ pool.worker_type }}</div>
    <div>Worker preemptible: {{ pool.preemptible }}</div>
    <div>Worker cores: <input name="worker_cores" value="{{ pool.worker_cores }}" /></div>
    <div>Worker boot disk size (in GB): <input name="boot_disk_size_gb" value="{{ pool.boot_disk_size_gb }}" /></div>
    <div>
      <label for="worker_local_ssd_data_disk">Worker should use a Local SSD: </label>
      <input type="checkbox"
             id="worker_local_ssd_data_disk"
             name="worker_local_ssd_data_disk"
             {% if pool.worker_local_ssd_data_disk %}checked{% endif %}
      value="true" />
    </div>
    <div>Worker External SSD data disk size (in GB): <input name="worker_external_ssd_data_disk_size_gb" value="{{ pool.worker_external_ssd_data_disk_size_gb }}" /></div>
    <div>Standing worker cores: <input name="standing_worker_cores" value="{{ pool.standing_worker_cores }}" /></div>
    <div>Min instances: <input name="min_instances" value="{{ pool.min_instances }}" /></div>
    <div>Max instances: <input name="max_instances" value="{{ pool.max_instances }}" /></div>
    <div>Max live instances: <input name="max_live_instances" value="{{ pool.max_live_instances }}" /></div>
    <div>Max new instances per autoscaler loop: <input name="max_new_instances_per_autoscaler_loop" value="{{ pool.max_new_instances_per_autoscaler_loop }}" /></div>
    <div>Autoscaler loop period in seconds: <input name="autoscaler_loop_period_secs" value="{{ pool.autoscaler_loop_period_secs }}" /></div>
    <div>Worker max idle time in seconds: <input name="worker_max_idle_time_secs" value="{{ pool.worker_max_idle_time_secs }}" /></div>
    <div>Standing worker max idle time in seconds: <input name="standing_worker_max_idle_time_secs" value="{{ pool.standing_worker_max_idle_time_secs }}" /></div>
    <div>Job queue scheduling window in seconds: <input name="job_queue_scheduling_window_secs" value="{{ pool.job_queue_scheduling_window_secs }}" /></div>
    <input type="hidden" name="_csrf" value="{{ csrf_token }}"/>
    <input type="hidden" name="_pool_config_json" value="{{ pool_config_json }}"/>
    <button>
      Update
    </button>
  </form>
</div>

<h2>User Resources</h2>
<table class="data-table" id="user_resources">
  <thead>
  <tr>
    <th>User</th>
    <th>Ready Jobs</th>
    <th>Ready Cores</th>
    <th>Allocated Cores</th>
    <th>Running Jobs</th>
    <th>Running Cores</th>
  </tr>
  </thead>
  <tbody>
  {% for user in user_resources %}
  <tr>
    <td>{{ user['user'] }}</td>
    <td class="numeric-cell">{{ user['n_ready_jobs'] }}</td>
    <td class="numeric-cell">{{ user['ready_cores_mcpu'] / 1000 }}</td>
    <td class="numeric-cell">{{ user['allocated_cores_mcpu'] / 1000 }}</td>
    <td class="numeric-cell">{{ user['n_running_jobs'] }}</td>
    <td class="numeric-cell">{{ user['running_cores_mcpu'] / 1000 }}</td>
  </tr>
  {% endfor %}
  </tbody>
</table>

<h2>Status</h2>
<div class="attributes">
  <div>Ready cores: {{ ready_cores_mcpu / 1000 }}</div>
</div>
<table class="data-table" id="status">
  <thead>
    <tr>
      <th colspan="4">Instances</th>
      <th></th>
      <th colspan="4">Cores</th>
      <th></th>
      <th colspan="3">Schedulable Cores</th>
    </tr>
    <tr>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
      <th></th>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
      <th></th>
      <th>Free</th>
      <th>Total</th>
      <th>% Free</th>
    </tr>
  </thead>
<tbody>
  <tr>
    <td class="numeric-cell">{{ pool.all_versions_instances_by_state['pending'] }}</td>
    <td class="numeric-cell">{{ pool.all_versions_instances_by_state['active'] }}</td>
    <td class="numeric-cell">{{ pool.all_versions_instances_by_state['inactive'] }}</td>
    <td class="numeric-cell">{{ pool.all_versions_instances_by_state['deleted'] }}</td>
    <td></td>
    <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['pending'] / 1000 }}</td>
    <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['active'] / 1000 }}</td>
    <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['inactive'] / 1000 }}</td>
    <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['deleted'] / 1000 }}</td>
    <td></td>
    <td class="numeric-cell">{{ pool.current_worker_version_stats.active_schedulable_free_cores_mcpu / 1000 }}</td>
    <td class="numeric-cell">{{ pool.current_worker_version_stats.cores_mcpu_by_state['active'] / 1000 }}</td>
    {% if pool.current_worker_version_stats.cores_mcpu_by_state['active'] != 0 %}
    <td class="numeric-cell">{{ (pool.current_worker_version_stats.active_schedulable_free_cores_mcpu * 100 / pool.current_worker_version_stats.cores_mcpu_by_state['active']) | round(1)}}%</td>
    {% else %}
    <td class="numeric-cell"></td>
    {% endif %}
  </tr>
  </tbody>
</table>

<h2>Instances</h2>
<table class="data-table" id="instances">
  <thead>
  <tr>
    <th>Name</th>
    <th>Location</th>
    <th>Version</th>
    <th>State</th>
    <th>Free Cores</th>
    <th>Failed Requests</th>
    <th>Time Created</th>
    <th>Last Updated</th>
  </tr>
  </thead>
  <tbody>
  {% for instance in instances %}
  <tr>
    <td>{{ instance.name }}</td>
    <td>{{ instance.location }}</td>
    <td class="numeric-cell">{{ instance.version }}</td>
    <td>{{ instance.state }}</td>
    <td class="numeric-cell">{{ instance.free_cores_mcpu / 1000 }} / {{ instance.cores_mcpu / 1000 }}</td>
    <td class="numeric-cell">{{ instance.failed_request_count }}</td>
    <td>{{ instance.time_created_str() }}</td>
    <td>{{ instance.last_updated_str() }} ago</td>
    <td>
      <form action="{{ base_path }}/instances/{{ instance.name }}/kill" method="post">
        <input type="hidden" name="_csrf" value="{{ csrf_token }}" />
        <button>Delete</button>
      </form>
    </td>
  </tr>
  {% endfor %}
  </tbody>
</table>
{% endblock %}
